package com.kkb.controller;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.kkb.bean.Express;
import com.kkb.bean.Message;
import com.kkb.bean.Result;
import com.kkb.bean.TableExpress;
import com.kkb.mvc.ResponseBody;
import com.kkb.service.AdminService;
import com.kkb.service.CourierService;
import com.kkb.service.ExpressService;
import com.kkb.service.UserService;
import com.kkb.utils.DateFormatUtil;
import com.kkb.utils.JSONUtil;
import com.kkb.utils.UserUtil;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ExpressController {

    @ResponseBody("/express/console.do")
    public String console(HttpServletRequest request , HttpServletResponse response){
        List<Map<String, Integer>> data = ExpressService.console();
        // 其他数据 用户新增 + 快递员新增
        Map<String, Integer> console_courier = CourierService.console();
        Map<String, Integer> console_user = UserService.console();
        data.add(console_courier);
        data.add(console_user);
        Message message =null;
        if (data.size() == 0){
            message = new Message(-1);
        } else{
            message = new Message(0,data);
        }
        return JSONUtil.toJSON(message);
    }

    @ResponseBody("/express/list.do")
    public String list(HttpServletRequest request , HttpServletResponse response){
        // 得到参数
        String offset = request.getParameter("offset");
        String pageNumber = request.getParameter("pageNumber");
        System.out.println(offset);
        System.out.println(pageNumber);
        // 先得到总数
        List<Map<String, Integer>> console = ExpressService.console();
        // 得到数据
        List<Express> all = ExpressService.findAll(true, (offset == null) ? 0: Integer.parseInt(offset), (pageNumber == null) ? console.get(0).get("data1_size"): Integer.parseInt(pageNumber));
        // 数据的格式转换
        ArrayList<TableExpress> newAll = new ArrayList<>();
        for (Express e : all) {
            String code = e.getCode()== null ? "已取件": e.getCode();
            String outtime = e.getOutTime()== null ? "未出库": DateFormatUtil.format(e.getOutTime());
            String intime = DateFormatUtil.format(e.getInTime());
            String status = e.getStatus() == 0 ? "未取件":"已出库";
            TableExpress tableExpress = new TableExpress(e.getId(),e.getNumber(),e.getUsername(),e.getUserPhone(),e.getCompany(),code,intime,outtime,status,e.getSysPhone());
            newAll.add(tableExpress);
        }
        Result<TableExpress> result = new Result<>(newAll,console.get(0).get("data1_size"));
        return JSONUtil.toJSON(result);
    }

    @ResponseBody("/express/insert.do")
    public String insert(HttpServletRequest request , HttpServletResponse response){
        String number = request.getParameter("number");
        String company = request.getParameter("company");
        String username = request.getParameter("username");
        String userPhone = request.getParameter("userPhone");
        Express e = new Express();
        e.setNumber(number);
        e.setCompany(company);
        e.setUsername(username);
        e.setUserPhone(userPhone);
        // TODO : 用户还未登录 用户信息未得到
        // 录入人手机号
        e.setSysPhone(UserUtil.getUserPhone(request.getSession()));
        boolean res = ExpressService.insert(e);
        Message m = null;
        if (res){
            m = new Message(0,"录入成功");
        } else{
            m = new Message(-1,"录入失败");
        }
        return JSONUtil.toJSON(m);
    }

    @ResponseBody("/express/find.do")
    public String find(HttpServletRequest request , HttpServletResponse response){
        String number = request.getParameter("number");
        Express express = ExpressService.findByNumber(number);
        Message e = null;
        if (express == null) {
            // 查询不到快递
            e = new Message(-1,"快递单号不存在");
        } else {
            e = new Message(0,"查询成功",express);
        }
        return JSONUtil.toJSON(e);
    }

    @ResponseBody("/express/update.do")
    public String update(HttpServletRequest request , HttpServletResponse response){
        int id = Integer.parseInt(request.getParameter("id"));
        String number = request.getParameter("number");
        String username = request.getParameter("username");
        String company = request.getParameter("company");
        String userPhone = request.getParameter("userPhone");
        int status = Integer.parseInt(request.getParameter("status"));
        Express newExpress = new Express();
        newExpress.setNumber(number);
        newExpress.setCompany(company);
        newExpress.setUsername(username);
        newExpress.setUserPhone(userPhone);
        newExpress.setStatus(status);
        System.out.println(userPhone);
        boolean update = ExpressService.update(id, newExpress);
        Message m = null;
        if (update) {
            m = new Message(0,"修改成功");
        } else {
            m = new Message(-1,"修改失败");
        }
        return JSONUtil.toJSON(m);
    }

    @ResponseBody("/express/delete.do")
    public String delete(HttpServletRequest request , HttpServletResponse response){
        int id = Integer.parseInt(request.getParameter("id"));

        boolean delete = ExpressService.delete(id);
        Message m = null;
        if (delete){
            m = new Message(0,"删除成功");
        } else {
            m = new Message(-1,"删除失败");
        }
        return JSON.toJSONString(m);
    }

    @ResponseBody("/express/isOneNumber.do")
    public String isOneNumber(HttpServletRequest request , HttpServletResponse response){

        String number = request.getParameter("number");
        boolean res = ExpressService.isOneNumber(number);
        Message m = null;
        if (res){
            // 存在
            m = new Message(-1);
        } else {
            m = new Message(0);
        }
        return JSON.toJSONString(m);
    }

}
